<?php
namespace app\service;

use app\model\MemberModel;
use app\model\mongo\OrderReviewMongo;
use app\model\OrderModel;
use app\model\OrderReviewModel;
use support\facade\Logger;
use const support\CORRECT;
use const support\DENY;
use const support\OPERATE_ERROR;
use const support\order_status_8;
use const support\PARAMETER_ERROR;

class OrderReviewService
{

    //添加评论
    public static function toAddOrderComment(int $memberId,int $orderId,int $goodsScore,string $goodsComment,string $goodsCommentImages=''): array
    {
        //查询是否符合退款要求
        $order=OrderModel::where([
            'id'        =>$orderId,
            'member_id' =>$memberId,
            'is_comment'=>DENY
        ])->whereIn('order_status',[order_status_8])->first();
        if (empty($order->id)){
            Logger::app('添加评论:无效订单');
            return formatReturnData(PARAMETER_ERROR,'该订单ID无效');
        }
        $ret=OrderReviewModel::insert([
            'order_id'		        =>$orderId,
            'goods_period_date'     =>$order->goods_period_date,
            'goods_period_title'    =>$order->goods_period_title,
            'member_id'		        =>$memberId,
            'goods_id'  	        =>$order->goods_id,
            'goods_score'           =>$goodsScore,
            'goods_comment'         =>$goodsComment,
            'goods_comment_images'  =>$goodsCommentImages,
        ]);
        if (!$ret){
            Logger::app('添加评论:添加评论无效');
            return formatReturnData(OPERATE_ERROR,'添加评论失败1');
        }
        $ret=OrderModel::where([
            'id'        =>$orderId,
            'member_id' =>$memberId,
        ])->update([
            'is_comment'=>CORRECT,
            'update_time'=>time()
        ]);
        if (!$ret){
            Logger::app('添加评论:添加评论无效');
            return formatReturnData(OPERATE_ERROR,'添加评论失败2');
        }
        return formatReturnSuccessData();
    }

    //评论满意度计算
    public static function reviewSatisfactionCalculate(int $goodsId){
        $goodsScores=OrderReviewModel::where(['goods_id'=>$goodsId,'status'=>CORRECT])->pluck('goods_score')->toArray();
        //计算总数量
        $totalNum=count($goodsScores);
        if (!$totalNum){
            return 100;
        }
        //计算总分数
        $totalScoreNum=$totalNum*5;
        $sumScoreNum=array_sum($goodsScores);
        //计算比例
        return ceil(($sumScoreNum/$totalScoreNum) * 100);
    }

    //评论列表
    public static function getReviews(int $goodsId,int $page,int $limit): array
    {
        $offset=($page-1)*$limit;
        $lists=OrderReviewModel::where(['goods_id'=>$goodsId,'status'=>CORRECT])->select([
            'goods_score','goods_comment','goods_comment_images','create_time','goods_period_date','goods_period_title','member_id'
        ])->offset($offset)->limit($limit)->orderBy('create_time','desc')->get()->toArray();
        //批量获取用户信息
        $memberIds=array_column($lists,'member_id');
        $members=MemberModel::whereIn('id',$memberIds)->select(['id','nickname','avatar'])->get()->toArray();
        $members=array_column($members,null,'id');
        foreach ($lists as &$list){
            $list['nickname']=$members[$list['member_id']]['nickname']??'';
            $list['avatar']=$members[$list['member_id']]['avatar']??'';
            $list['goods_period_title']=$list['goods_period_title']??'';
        }
        return $lists;
    }

    //计算各分数分布比重
    public static function getReviewScoreRates(int $goodsId): array
    {
        $goodsScores=OrderReviewModel::where(['goods_id'=>$goodsId,'status'=>CORRECT])->pluck('goods_score')->toArray()?:[];
        //计算总数量
        $totalNum=count($goodsScores);
        $data=[];
        $list=$goodsScores?array_count_values($goodsScores):[];
        for ($i=5;$i>0;$i--){
            $data[]=$totalNum?($list[$i]??0)/$totalNum:0;
        }
        return $data;
    }

}